import { Controller, UseGuards } from '@nestjs/common';
import { Crud, CrudController, Override, ParsedRequest, CrudRequest, ParsedBody, CrudAuth } from '@nestjsx/crud';
import { ApiTags, ApiBody, ApiBearerAuth} from '@nestjs/swagger';
import { Comment } from '../../entities/comment.entity';
import { CommentService } from '../../services/comment/comment.service';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';

@Crud({
  model:{
    type:Comment,
  },
  query:{
    join:{
      commentPicture:{
        //allow:["id"],
        exclude:["comment_id","sort_order"],
        eager:true
      }
    },
    exclude:["user_id"]
  },
  routes:{
    only:['createOneBase','deleteOneBase','getManyBase']
  }
})
@CrudAuth({
  property:"user",
  filter: (user) => ({
    user_id: user.userId
  }),
  persist:(user) => {
    return user;
  }
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('comment')
@Controller('api/comment')
export class CommentController implements CrudController<Comment>{
  constructor(
    public service:CommentService
  ){}

  get base(): CrudController<Comment> {
    return this;
  }

  @ApiBody({
    type: Comment
  })
  @Override()
  createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Comment): Promise<Comment> {
    dto.user_id=req.parsed.authPersist.userId;
    dto.add_time=moment().unix();
    return this.base.createOneBase(req, dto);
  }

  @ApiBody({
    type: Comment
  })
  @Override()
  updateOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: any): Promise<Comment> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.updateOneBase(req, dto);
  }
}
